using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for MailMerge
/// </summary>
public class MailMerge
{

    private string _mFullName = String.Empty;
    private string _mOwnerName = String.Empty;
    private string _mEmail = String.Empty;
    private string _mPAN = String.Empty;
    private string _mYear = String.Empty;
    private string _mDocumentPath = String.Empty;
    private string _mSubject = String.Empty;
    private string _mTemplateName = String.Empty;
    private string _mBody = String.Empty;
    private string _mDocumentName = String.Empty;
    private string _mUpdatedBy = String.Empty;
    private string _mStatusMessage = String.Empty;
    private string _mStatusComments = String.Empty;
    private string _mStatusDateTime = String.Empty;
    private string _mCustomURL = String.Empty;

    // TODO : DocumentName, Subject
    #region Readonly Properties
    
    public string Body
    {
        get { return _mBody; }
    }
    
    public string TemplateName
    {
        get { return _mTemplateName==""? "default":_mTemplateName; }
    }

    public string Subject
    {
        get { return _mSubject==""?URL:_mSubject ; }
    }

    public string URL
    {
        get { return System.Configuration.ConfigurationManager.AppSettings.Get("URLSpecifiedInMail"); }
    }

    public string CustomURL
    {
        get { return _mCustomURL; }
        set { _mCustomURL = value; }
    }
    public string PANNOTUPDATED
    {
        get { return _mPAN == "" ? "PAN Number not updated" : _mPAN; }
    }
    public string AdminEmailAddress
    {
        get { return System.Configuration.ConfigurationManager.AppSettings.Get("UNSUBMAIL"); }
    }
    #endregion

    #region public Properties

    public string UpdatedBy
    {
        get { return _mUpdatedBy; }
        set { _mUpdatedBy = value; }
    }
    public string StatusMessage
    {
        get { return _mStatusMessage; }
        set { _mStatusMessage = value; }
    }
    public string StatusComments
    {
        get { return _mStatusComments; }
        set { _mStatusComments = value; }
    }
    public string StatusDateTime
    {
        get { return _mStatusDateTime; }
        set { _mStatusDateTime = value; }
    }
    public string FullName
    {
        get { return _mFullName; }
        set { _mFullName = value; }
    }

    
    public string OwnerName
    {
        get { return _mOwnerName == "" ? "<Not Updated. Please recheck in website>" : _mOwnerName;  }
        set { _mOwnerName = value; }
    }
    public string Email
    {
        get { return _mEmail == "" ? "<Not Updated. Please recheck in website>" : _mEmail; }
        set { _mEmail = value; }
    }
    public string PAN
    {
        get { return _mPAN; }
        set { _mPAN = value; }
    }
   
    public string Year
    {
        get { return _mYear; }
        set { _mYear = value; }
    }
    public string DocumentPath
    {
        get { return System.Configuration.ConfigurationManager.AppSettings.Get("URLSpecifiedInMail") + "/" + _mDocumentPath; }
        set { _mDocumentPath = value; }
    }

    public string DocumentName
    {
        get { return _mDocumentName; }
        set { _mDocumentName = value; }
    }


#endregion



    public MailMerge()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public MailMerge(string templateName)
    {
        _mTemplateName = templateName;
    }

    public MailMerge(string fullName, string ownerName, string sEmail, string sPAN, string sYear, string documentName, string documentPath, string templateName)
    {
        _mFullName = fullName;
        _mOwnerName = ownerName;
        _mEmail = sEmail;
        _mPAN = sPAN;
        _mYear = sYear;
        _mDocumentName = documentName;
        _mDocumentPath = documentPath;
        _mTemplateName = templateName;

        GetMailDetails();
    }

    public  void  GetMailDetails()
    {
        System.Text.StringBuilder sBody = new System.Text.StringBuilder();

        // Get User Details
        System.Data.SqlClient.SqlDataReader dr = mojoPortal.Data.SqlHelper.ExecuteReader
           (System.Configuration.ConfigurationManager.AppSettings.Get("MSSQLConnectionString")
           , CommandType.StoredProcedure, "uspGetMailBody",
           new System.Data.SqlClient.SqlParameter("@TemplateName", TemplateName));
        if (dr.HasRows)
        {
            dr.Read();
            sBody.Append(dr["body"].ToString());
            sBody.Replace("#FullName#", FullName);
            sBody.Replace("#URL#", URL );
            sBody.Replace("#OwnerName#", OwnerName );
            sBody.Replace("#Email#", Email );
            sBody.Replace("#PAN#", PAN );
            sBody.Replace("#PANNOTUPDATED#", PANNOTUPDATED);
            sBody.Replace("#Year#", Year );
            sBody.Replace("#DocumentName#", DocumentName);
            sBody.Replace("#DocumentPath#", DocumentPath );
            sBody.Replace("#AdminEmailAddress#", AdminEmailAddress );
            sBody.Replace("#UpdatedBy#", UpdatedBy );
            sBody.Replace("#StatusMessage#", StatusMessage);
            sBody.Replace("#Statuscomments#", StatusComments);
            sBody.Replace("#StatusDateTime#", StatusDateTime);
            sBody.Replace("#CustomURL#", CustomURL);
            

            _mSubject = dr["subject"].ToString();
        }
        dr.Close();

        _mBody = sBody.ToString();
    }
}
